package com.rd.student.dashuju.wangbo.exercise02;

import java.util.HashMap;
import java.util.Map;

public class RomeOfInt {
    public static void main(String[] args) {
        System.out.println(RomanToInt("VI"));
    }
    public static int RomanToInt(String s){
        Map<String,Integer> map = new HashMap<>();
        int result = 0;
        map.put("I",1);
        map.put("V",5);
        map.put("X",10);
        map.put("L",50);
        map.put("C",100);
        map.put("D",500);
        map.put("M",1000);
        if(s.contains("IV")){
            result+=4;
            s = s.replace("IV","");
        }
        if(s.contains("IX")){
            result+=9;
            s = s.replace("IX","");
        }
        if(s.contains("XL")){
            result+=40;
            s = s.replace("XL","");
        }
        if(s.contains("XC")){
            result+=90;
            s = s.replace("XC","");
        }
        if(s.contains("CD")){
            result+=400;
            s = s.replace("CD","");
        }
        if(s.contains("CM")){
            result+=900;
            s = s.replace("CM","");
        }
        int n = s.length();
        for (int i = 0; i < n; i++) {
            String s1 = String.valueOf(s.charAt(i));
            result += map.get(s1);
        }
        return result;
    }
}
